package day_17.FanSheGouZao;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //m1();
        //m2();
        //m3();
        m4();
    }

    private static void m4() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //获取一个私有的构造方法并创建对象
        //1.获取class对象
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        //2.获取一个私有化的构造方法.
        Constructor constructor = aClass.getDeclaredConstructor(String.class);
        //被private修饰的成员,不能直接使用的
        //如果用反射强行获取并使用,需要临时取消访问检查
        constructor.setAccessible(true);
        //3.直接创建对象
        Student student =(Student) constructor.newInstance("张某");
        System.out.println(student);
    }

    private static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //简写格式   过时方法！！！！！！！！！！！！！
        //1.获取class对象
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        //2.在Class类中,有一个newInstance方法,可以利用空参直接创建一个对象
        Student student =(Student) aClass.newInstance();
        System.out.println(student);
    }

    private static void m2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //1.获取class对象
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        //2.获取构造方法对象
        Constructor constructor = aClass.getConstructor();
        //3.利用空参来创建Student的对象
        Student student = (Student) constructor.newInstance();
        System.out.println(student);
    }

    private static void m1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //1.获取class对象
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        //2.获取构造方法对象
        Constructor constructor = aClass.getConstructor(String.class, int.class);
        //3.利用newInstance创建Student的对象
        Student student= (Student) constructor.newInstance("张某", 23);
        System.out.println(student);
    }
}
class lx01{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //m1();
        //m2();
        m3();
    }

    private static void m3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        Constructor constructor = aClass.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        Student student =(Student) constructor.newInstance("李某");
        System.out.println(student);
    }

    private static void m2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        Constructor constructor = aClass.getConstructor();
        Student student =(Student) constructor.newInstance();
        System.out.println(student);
    }

    private static void m1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("day_17.FanSheGouZao.Student");
        Constructor constructor = aClass.getConstructor(String.class, int.class);
        Student student =(Student) constructor.newInstance("李某", 21);
        System.out.println(student);
    }
}
